﻿namespace Zj.OpenAuth.Repository.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    using System.Linq;
    using Zj.OpenAuth.Domain.Core;
    using Zj.OpenAuth.Repository.Models.Mapping;

    public class OpenAuthDbContext : DbContext
    {
        //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
        //使用“OpenAuthDbContext”连接字符串。默认情况下，此连接字符串针对您的 LocalDb 实例上的
        //“Zj.OpenAuth.Repository.Models.OpenAuthDbContext”数据库。
        // 
        //如果您想要针对其他数据库和/或数据库提供程序，请在应用程序配置文件中修改“OpenAuthDbContext”
        //连接字符串。
        public OpenAuthDbContext()
            : base("name=OpenAuthDbContext")
        {
            Database.SetInitializer<OpenAuthDbContext>(new CreateDatabaseIfNotExists<OpenAuthDbContext>());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Configurations.Add(new DepartmentMap());
            modelBuilder.Configurations.Add(new RoleMap());
            modelBuilder.Configurations.Add(new UserMap());
            modelBuilder.Configurations.Add(new MenuMap());
            modelBuilder.Configurations.Add(new UserRoleMappingMap());
            modelBuilder.Configurations.Add(new RoleMenuMappingMap());
        }
        //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First  模型
        //的详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。

        public virtual DbSet<Department> Department { get; set; }
        public virtual DbSet<Menu> Menu { get; set; }
        public virtual DbSet<Role> Role { get; set; }
        public virtual DbSet<User> User { get; set; }


        public virtual DbSet<UserRoleMapping> UserRoleMapping { get; set; }

        public virtual DbSet<RoleMenuMapping> RoleMenuMapping { get; set; }
    }

}